package com.cc;

import com.alibaba.cloud.ai.dashscope.chat.DashScopeChatModel;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.chat.prompt.ChatOptions;
import org.springframework.ai.deepseek.DeepSeekChatModel;
import org.springframework.ai.ollama.OllamaChatModel;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

import java.util.HashMap;

@RestController
public class TestController {

    HashMap<String, ChatModel> platforms = new HashMap<>();

    public TestController(
            DeepSeekChatModel deepSeekChatModel,
            OllamaChatModel ollamaChatModel,
            DashScopeChatModel dashScopeChatModel
    ) {
        platforms.put("deepseek", deepSeekChatModel);
        platforms.put("ollama", ollamaChatModel);
        platforms.put("ali", dashScopeChatModel);
    }
    //http://localhost:8080/chat?message=你是谁&platform=ollama&model=qwen3:4b&temperature=1.5
    @RequestMapping(value = "/chat", produces = "text/stream;charset=UTF-8")
    public Flux<String> chat(String message, ModelDto modelDto) {
        String platform = modelDto.getPlatform();
        // 获取需要使用的模型
        ChatModel chatModel = platforms.get(platform);
        // 根据模型创建chatClient
        ChatClient.Builder builder = ChatClient.builder(chatModel);
        // 创建chatClient参数
        ChatOptions options = ChatOptions.builder()
                .temperature(modelDto.getTemperature())
                .model(modelDto.getModel())
                .build();
        // 给chatClient添加参数
        ChatClient build = builder.defaultOptions(options).build();
        // 连接获取内容
        return build.prompt()
                .user(message)
                .stream()
                .content();
    }
}
